import Taro from '@tarojs/taro'
import { getTop, recommend } from '../../services/post'
import { parsePost } from '../../utils/parse'

const namespace = 'page_recommend'
export { namespace }
export default {
  namespace,
  state: {
    state: 0,
    refreshDate: new Date(),
    loadDate: new Date(),
    isLoadData: false,
    hasMore: true,
    items: [],
    topItems: [],
    pageIndex: 1,
    pageSize: 5,
    count: 0
  },
  effects: {
    *init(_, { select, put, call }) {
      let pageSize = yield select((state) => state[namespace].pageSize)
      let pageIndex = yield select((state) => state[namespace].pageIndex)
      const loadDate = yield select((state) => state[namespace].loadDate)
      const items = yield select((state) => state[namespace].items)
      try {
        let res = yield call(getTop)
        console.log(res)
        if (res.code === 0) {
          const topItems = parsePost(res.data.items)
          yield put({
            type: 'setState',
            payload: {
              topItems: topItems
            }
          })
        }
        res = yield call(recommend, loadDate, 1, 1, pageSize)
        console.log(res)
        if (res.code === 0) {
          const index = pageIndex + 1
          const parseItems = parsePost(res.data.items)
          yield put({
            type: 'setState',
            payload: {
              items: [...items, ...parseItems],
              pageIndex: index,
              state: 3
            }
          })
        } else {
          yield put({
            type: 'setState',
            payload: {
              state: 1
            }
          })
        }
      } catch (error) {}
    },
    *loadData(payload, { select, put, call }) {
      let pageSize = yield select((state) => state[namespace].pageSize)
      let pageIndex = yield select((state) => state[namespace].pageIndex)
      let items = yield select((state) => state[namespace].items)
      const isLoadData = yield select((state) => state[namespace].isLoadData)
      const hasMore = yield select((state) => state[namespace].hasMore)
      const loadDate = yield select((state) => state[namespace].loadDate)
      const refreshDate = yield select((state) => state[namespace].refreshDate)
      const direction = payload.payload.direction
      if (isLoadData) {
        return
      }
      yield put({
        type: 'setState',
        payload: {
          isLoadData: true
        }
      })
      try {
        if (direction === 1) {
          if (hasMore) {
            const res = yield call(recommend, loadDate, 1, pageIndex, pageSize)
            console.log(res)
            if (res.code === 0) {
              const more = res.data.items.length === pageSize
              const index = pageIndex + 1
              const parseItems = parsePost(res.data.items)
              yield put({
                type: 'setState',
                payload: {
                  items: [...items, ...parseItems],
                  pageIndex: index,
                  hasMore: more
                }
              })
            }
          }
        } else {
          const res = yield call(recommend, refreshDate, 0, 1, pageSize)
          console.log(res)
          if (res.code === 0) {
            for (const item of res.data.items) {
              items.unshift(item)
            }
          }
          yield put({
            type: 'setState',
            payload: {
              items: items,
              count: res.data.items.length,
              refreshDate: new Date()
            }
          })
        }
        yield put({
          type: 'setState',
          payload: {
            isLoadData: false
          }
        })
      } catch (error) {}
    }
  },
  reducers: {
    setState(state, { payload }) {
      return { ...state, ...payload }
    }
  }
}
